package com.zcoj.controller.Admin;

import com.zcoj.core.model.ApiResponse;
import com.zcoj.mail.pojo.Mail;
import com.zcoj.mail.service.MailService;
import com.zcoj.pojo.Dto.Filing;
import com.zcoj.pojo.Dto.ServerMetrics;
import com.zcoj.service.FilingService;
import com.zcoj.service.ImageService;
import com.zcoj.service.ServerMetricsService;
import com.zcoj.service.SystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/5/23 09:52
 */
@RestController
@PreAuthorize("hasAnyAuthority('super_admin','admin')")
@RequestMapping("/system")
public class SystemInfoController {


    @Autowired
    private ImageService imageService;

    @Autowired
    private ServerMetricsService serverMetricsService;

    @Autowired
    private MailService mailService;

    @Autowired
    private FilingService filingService;
    @Autowired
    private SystemInfoService systemInfoService;

    @GetMapping("/getSystemInfo")
    public ApiResponse getSystemInfo(){
        ServerMetrics serverMetrics = serverMetricsService.getServerMetrics();
        return ApiResponse.success(serverMetrics);
    }

    @GetMapping("/getAdminHomeData")
    public ApiResponse getAdminHomeData(){
        return ApiResponse.success(systemInfoService.getAdminHomeData());
    }


    @GetMapping("/getMailInfo")
    public ApiResponse getMainInfo(){
        return ApiResponse.success(mailService.getMailData().get(0));
    }

    @GetMapping("/getFilingInfo")
    public ApiResponse getFilingInfo(){
        return ApiResponse.success(filingService.findOneFiling());
    }

    @PostMapping("/updateMailInfo")
    public ApiResponse updateMailInfo(@RequestBody Mail mail ){
        Integer integer = mailService.updateMail(mail);
        return integer>0?ApiResponse.success("邮箱信息更新成功"):ApiResponse.failed("邮箱信息更新失败");
    }

    @PostMapping("/updateFilingInfo")
    public ApiResponse updateFilingInfo(@RequestBody Filing filing ){
        Integer integer = filingService.updateFiling(filing);
        return integer>0?ApiResponse.success("备案号信息更新成功"):ApiResponse.failed("备案号信息更新失败");
    }

    @PostMapping("/setImageBase64")
    public ApiResponse setImageBase64(@RequestBody Map<String, String> map){
        String imageBase64 = map.get("imageBase64Number");

        imageService.modifyImageBase64(imageBase64);
        return ApiResponse.success(null);
    }


}
